<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开关灯效果</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        #light {
            width: 100px;
            height: 150px;
            transition: opacity 0.5s ease;
        }
        .off {
            opacity: 0.3;
        }
        .on {
            opacity: 1;
        }
    </style>
</head>
<body>
    <h1>点击按钮开关灯</h1>
    <button id="toggleButton">打开灯</button>
    <br><br>
    <img id="light" src="img/on.jpg " alt="灯" class="off">
    <script>
        // 获取按钮和灯的元素
        const button = document.getElementById('toggleButton');
        const light = document.getElementById('light');

        // 初始化灯的状态
        let isLightOn = false;

        // 切换灯的状态
        button.addEventListener('click', () => {
            isLightOn = !isLightOn; // 切换状态

            if (isLightOn) {
                // 如果灯是开的
                light.classList.add('on');
                light.classList.remove('off');
                button.textContent = '关闭灯';
            } else {
                // 如果灯是关的
                light.classList.add('off');
                light.classList.remove('on');
                button.textContent = '打开灯';
            }
        });
    </script>
</body>
</html>